﻿/*
 * My Timer - FormAddEditAlarem.cs
 * 
 * Copyright (C) 2009 Ahmad Hanis Azmi
 * 
 * This file is part of My Timer
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace My_Timer
{
    public partial class FormAddEditAlarm : Form
    {
        public FormAddEditAlarm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        static object focusObject = null;

        /// <summary>
        /// temporary time holder
        /// </summary>
        static TempTime tempTime;

        /// <summary>
        /// temporary time holder
        /// </summary>
        struct TempTime
        {
            public byte Hour;
            public byte Minute;
            public byte Second;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormAddEditAlarm_Load(object sender, EventArgs e)
        {
            switch (Alarm.addOrEdit)
            {
                case 0:
                    comboBoxTimeValue.SelectedIndex = 0;

                    numericUpDownHour.Value = DateTime.Now.Hour;
                    numericUpDownMinutes.Value = DateTime.Now.Minute;
                    numericUpDownSecond.Value = DateTime.Now.Second;

                    break;

                case 1:

                    break;

                default:

                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxAlarmDayAll_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxAlarmDayAll.Checked == true)
            {
                checkBoxAlarmDaySunday.Enabled = false;
                checkBoxAlarmDayMonday.Enabled = false;
                checkBoxAlarmDayTuesday.Enabled = false;
                checkBoxAlarmDayWednesday.Enabled = false;
                checkBoxAlarmDayThursday.Enabled = false;
                checkBoxAlarmDayFriday.Enabled = false;
                checkBoxAlarmDaySaturday.Enabled = false;
            }
            else
            {
                checkBoxAlarmDaySunday.Enabled = true;
                checkBoxAlarmDayMonday.Enabled = true;
                checkBoxAlarmDayTuesday.Enabled = true;
                checkBoxAlarmDayWednesday.Enabled = true;
                checkBoxAlarmDayThursday.Enabled = true;
                checkBoxAlarmDayFriday.Enabled = true;
                checkBoxAlarmDaySaturday.Enabled = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxShowMsgbox_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxShowMsgbox.Checked == true)
            {
                textBoxMsgbox.ReadOnly = false;
            }
            else
            {
                textBoxMsgbox.ReadOnly = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxPlayAudio_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxPlayAudio.Checked == true)
            {
                buttonBrowseAudio.Enabled = true;
            }
            else
            {
                buttonBrowseAudio.Enabled = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxRun_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxRun.Checked == true)
            {
                textBoxRun.ReadOnly = false;
                buttonBrowseRun.Enabled = true;
            }
            else
            {
                textBoxRun.ReadOnly = true;
                buttonBrowseRun.Enabled = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxAlarmTitle_Enter(object sender, EventArgs e)
        {
            textBoxAlarmTitle.SelectAll();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxAlarmTitle_Click(object sender, EventArgs e)
        {
            if ((textBoxAlarmTitle.SelectionLength == 0) && (focusObject != textBoxAlarmTitle))
            {
                textBoxAlarmTitle.SelectAll();
            }
            
            focusObject = textBoxAlarmTitle;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxAlarmTitle_Leave(object sender, EventArgs e)
        {
            focusObject = null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDownHour_Click(object sender, EventArgs e)
        {
            numericUpDownHour.Select(0, numericUpDownHour.Value.ToString().Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDownHour_Enter(object sender, EventArgs e)
        {
            numericUpDownHour.Select(0, numericUpDownHour.Value.ToString().Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDownMinutes_Click(object sender, EventArgs e)
        {
            numericUpDownMinutes.Select(0, numericUpDownMinutes.Value.ToString().Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDownMinutes_Enter(object sender, EventArgs e)
        {
            numericUpDownMinutes.Select(0, numericUpDownMinutes.Value.ToString().Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDownSecond_Click(object sender, EventArgs e)
        {
            numericUpDownSecond.Select(0, numericUpDownSecond.Value.ToString().Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDownSecond_Enter(object sender, EventArgs e)
        {
            numericUpDownSecond.Select(0, numericUpDownSecond.Value.ToString().Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonBrowseAudio_Click(object sender, EventArgs e)
        {
            OpenFileDialog r_Browse = new OpenFileDialog();
            r_Browse.Title = "Select audio file";
            r_Browse.Filter = "MP3 Audio (*.MP3)|*.mp3";
            r_Browse.FilterIndex = 2;
            r_Browse.RestoreDirectory = true;

            if (r_Browse.ShowDialog() == DialogResult.OK)
            {
                buttonBrowseAudio.Text = r_Browse.FileName;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonBrowseRun_Click(object sender, EventArgs e)
        {
            OpenFileDialog r_Browse = new OpenFileDialog();
            r_Browse.Title = "Select file";
            r_Browse.Filter = "All Files (*.*)|*.*";
            r_Browse.FilterIndex = 2;
            r_Browse.RestoreDirectory = true;

            if (r_Browse.ShowDialog() == DialogResult.OK)
            {
                textBoxRun.Text = r_Browse.FileName;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxTimeValue_SelectedIndexChanged(object sender, EventArgs e)
        {
            byte hour = tempTime.Hour;

            switch (comboBoxTimeValue.SelectedIndex)
            {
                case 0:
                    numericUpDownHour.Maximum = 23;
                    numericUpDownHour.Minimum = 0;
                    numericUpDownHour.Value = (decimal)tempTime.Hour;

                    break;

                case 1:
                    if (hour > 12)
                    {
                        hour = (byte)(hour - 12);
                    }
                    if (hour == 0)
                    {
                        hour = 12;
                    }
                    numericUpDownHour.Value = hour;
                    numericUpDownHour.Maximum = 12;
                    numericUpDownHour.Minimum = 1;

                    break;

                case 2:
                    if (hour > 12)
                    {
                        hour = (byte)(hour - 12);
                    }
                    if (hour == 0)
                    {
                        hour = 12;
                    }
                    numericUpDownHour.Value = hour;
                    numericUpDownHour.Maximum = 12;
                    numericUpDownHour.Minimum = 1;

                    break;

                default:
                    comboBoxTimeValue.SelectedIndex = 0;
                    numericUpDownHour.Maximum = 23;
                    numericUpDownHour.Minimum = 0;
                    numericUpDownHour.Value = (decimal)tempTime.Hour;

                    break;
            }

            updateTempTime();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxShutdownComputer_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxShutdownComputer.Checked == true)
            {
                checkBoxRestartComputer.Checked = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxRestartComputer_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxRestartComputer.Checked == true)
            {
                checkBoxShutdownComputer.Checked = false;
            }
        }

        /// <summary>
        /// Update tempTime value
        /// </summary>
        private void updateTempTime()
        {
            byte hour = (byte)numericUpDownHour.Value;

            //convert to 24h before store, epecially if user select AM or PM
            switch (comboBoxTimeValue.SelectedIndex)
            {
                case 0:
                    tempTime.Hour = (byte)numericUpDownHour.Value;
                    break;

                case 1:
                    if (hour == 12)
                    {
                        hour = 0;
                    }
                    tempTime.Hour = hour;

                    break;

                case 2:
                    hour = (byte)(hour + 12);
                    if (hour == 24)
                    {
                        hour = 0;
                    }
                    tempTime.Hour = hour;

                    break;

                default:
                    comboBoxTimeValue.SelectedIndex = 0;
                    numericUpDownHour.Maximum = 23;
                    numericUpDownHour.Minimum = 0;
                    tempTime.Hour = (byte)numericUpDownHour.Value;

                    break;
            }


            tempTime.Minute = (byte)numericUpDownMinutes.Value;
            tempTime.Second = (byte)numericUpDownSecond.Value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDownHour_ValueChanged(object sender, EventArgs e)
        {
            updateTempTime();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDownMinutes_ValueChanged(object sender, EventArgs e)
        {
            updateTempTime();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDownSecond_ValueChanged(object sender, EventArgs e)
        {
            updateTempTime();
        }

        /// <summary>
        /// Add new alarm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textBoxAlarmTitle.Text.Length == 0)
            {
                textBoxAlarmTitle.Text = "<empty>";
            }

            // update temp time first
            updateTempTime();

            AlarmType alarm = new AlarmType();
            string s = "";

            switch (Alarm.addOrEdit)
            {
                case 0:
                    // set default value first
                    Alarm.setAlarmDefaultValue();

                    // adding value from user input
                    alarm.name = textBoxAlarmTitle.Text;

                    alarm.alarmTime.hours = tempTime.Hour;
                    alarm.alarmTime.minutes = tempTime.Minute;
                    alarm.alarmTime.seconds = tempTime.Second;

                    alarm.alarmDay.sunday = checkBoxAlarmDaySunday.Checked;
                    alarm.alarmDay.monday = checkBoxAlarmDayMonday.Checked;
                    alarm.alarmDay.tuesday = checkBoxAlarmDayTuesday.Checked;
                    alarm.alarmDay.wednesday = checkBoxAlarmDayWednesday.Checked;
                    alarm.alarmDay.thursday = checkBoxAlarmDayThursday.Checked;
                    alarm.alarmDay.friday = checkBoxAlarmDayFriday.Checked;
                    alarm.alarmDay.saturday = checkBoxAlarmDaySaturday.Checked;

                    if (checkBoxAlarmDayAll.Checked == true)
                    {
                        alarm.alarmDay.sunday = true;
                        alarm.alarmDay.monday = true;
                        alarm.alarmDay.tuesday = true;
                        alarm.alarmDay.wednesday = true;
                        alarm.alarmDay.thursday = true;
                        alarm.alarmDay.friday = true;
                        alarm.alarmDay.saturday = true;
                    }

                    alarm.messageBox.isEnabled = checkBoxShowMsgbox.Checked;
                    alarm.messageBox.message = textBoxMsgbox.Text;

                    alarm.playAudio.isEnabled = checkBoxPlayAudio.Checked;

                    if (buttonBrowseAudio.Text != "Browse...")
                    {
                        alarm.playAudio.audioPath = buttonBrowseAudio.Text;
                    }

                    alarm.shutdownCode = 0;

                    if (checkBoxShutdownComputer.Checked == true)
                    {
                        alarm.shutdownCode = 1;
                    }

                    if (checkBoxRestartComputer.Checked == true)
                    {
                        alarm.shutdownCode = 2;
                    }

                    alarm.runApplication.isEnabled = checkBoxRun.Checked;
                    alarm.runApplication.runPath = textBoxRun.Text;

                    // here we go, adding alarm to alarm list

                    s = numericUpDownHour.Value.ToString();
                    s = s + ":" + numericUpDownMinutes.Value.ToString();
                    s = s + ":" + numericUpDownSecond.Value.ToString();
                    s = s + " " + comboBoxTimeValue.SelectedText;

                    //todo: add alarm day to s

                    // 1. Add alarm to listview list first, mark it with the nextKey.
                    // 2. Store the key index of the alarm on the listview to alarm.keyIndex
                    // 3. tag the alarm on listview with nextKey.
                    // 4. Add alarm to alarm list
                    // 5. Increase the nextKey.

                    GlobalClass.r_frmMain.listViewAlarms.Items.Add(GlobalClass.nextKey.ToString(), 
                        alarm.name, null).SubItems.Add(s);

                    alarm.keyIndex = GlobalClass.r_frmMain.listViewAlarms.Items.IndexOfKey
                        (GlobalClass.nextKey.ToString());

                    GlobalClass.r_frmMain.listViewAlarms.Items[alarm.keyIndex].Tag = GlobalClass.nextKey;

                    Alarm.alarmList.Add(GlobalClass.nextKey, alarm);
                    GlobalClass.nextKey++;

                    break;

                case 1:

                    break;

                default:

                    break;
            }

            this.Close();
            this.Dispose();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
    }
}
